from typing import Dict, Any
from pathlib import Path
import json
import os
from dotenv import load_dotenv

class Config:
    """Configuration management for PolyModel Agent"""
    
    def __init__(self):
        self.BASE_DIR = Path(__file__).parent.parent
        self.MODEL_CACHE_DIR = self.BASE_DIR / "cache/models"
        self.MAX_MEMORY_ENTRIES = 100
        self.DEFAULT_LANGUAGE = "en"
        
        # Load environment variables
        load_dotenv()
        self.HF_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
        
        # Load secrets if available
        self.secrets = self._load_secrets()
        
        # Set up Hugging Face credentials
        if self.HF_API_KEY:
            os.environ["HUGGINGFACE_TOKEN"] = self.HF_API_KEY
            
    def _load_secrets(self) -> Dict[str, Any]:
        """Load secrets from secrets.json"""
        secrets_path = self.BASE_DIR / "config/secrets.json"
        if secrets_path.exists():
            try:
                with open(secrets_path) as f:
                    content = f.read().strip()
                    return json.loads(content) if content else {}
            except json.JSONDecodeError:
                self.logger.warning("secrets.json is empty or invalid, using empty dict")
                return {}
        return {}
    
    @property
    def model_configs(self) -> Dict[str, Dict]:
        """Get model configurations"""
        return {
            "chat": {
                "default": "gpt2",
                "available": ["gpt2", "gpt4", "falcon"],
                "parameters": {
                    "max_length": 1024,
                    "temperature": 0.7
                }
            },
            "translation": {
                "default": "Helsinki-NLP/opus-mt-en-zh",
                "available": {
                    ("en", "zh"): "Helsinki-NLP/opus-mt-en-zh",
                    ("zh", "en"): "Helsinki-NLP/opus-mt-zh-en",
                    ("en", "fr"): "Helsinki-NLP/opus-mt-en-fr",
                    ("fr", "en"): "Helsinki-NLP/opus-mt-fr-en"
                },
                "parameters": {
                    "max_length": 512,
                    "num_beams": 5,
                    "length_penalty": 1.0,
                    "temperature": 0.7
                }
            }
        } 